package com.hscm.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hscm.boss.common.consants.SysConstants;
import com.hscm.boss.common.enums.ResponseCode;
import com.hscm.boss.convert.PaymentConvert;
import com.hscm.boss.dao.entity.PayProduct;
import com.hscm.boss.dao.mapper.PayProductMapper;
import com.hscm.boss.dao.result.DicEntity;
import com.hscm.boss.mvc.model.AjaxResult;
import com.hscm.boss.mvc.model.from.product.PayProductFrom;
import com.hscm.boss.mvc.model.pages.PageInfo;
import com.hscm.boss.mvc.model.vo.product.MatchPayProductVo;
import com.hscm.boss.mvc.model.vo.product.PayProductVo;
import com.hscm.boss.mvc.service.PayProductService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class PayProductController extends BaseController {


    @Resource
    private PayProductService payProductService;

    @Resource
    private PaymentConvert paymentConvert;

    @GetMapping("/payment/product/pageList")
    public AjaxResult pageList(Integer pagNo) {
        pagNo = defPageNo(pagNo);
        IPage<PayProduct> pageResult = payProductService.pageList(pagNo, SysConstants.PAGE_SIZE);

        //创建分页对象
        PageInfo pageInfo = new PageInfo(pageResult.getCurrent(), pageResult.getPages()); //取到getCurrent当前页 和 getPages总页数  （注意这里和getTotal 总记录数）
        List<PayProduct> payProductList = pageResult.getRecords(); //取到分页中的数据

        Map<String, Object> data = new HashMap<>();
        data.put("page", pageInfo);
        List<PayProductVo> listPayProductVo = paymentConvert.toListPayProductVo(payProductList);
        data.put("list", listPayProductVo);

        // 演示前端的遮罩
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        return AjaxResult.ok(data);

    }

    /*新增支付产品*/
    @PostMapping("/payment/product/add")
    public AjaxResult addProduct(@RequestBody @Valid PayProductFrom payProductFrom) {
        payProductService.addProduct(payProductFrom);
        return AjaxResult.ok();
    }

    /*支付产品的字典列表*/
    @GetMapping("/payment/product/dicList")
    private AjaxResult queryProductDicList() {
        List<DicEntity> dicEntityList = payProductService.queryProductDicList();
        return AjaxResult.ok(dicEntityList);
    }


    /*修改禁用还是启用*/
    @PostMapping("/payment/product/status")
    public AjaxResult editStatusProduct(@RequestParam String productCode) {
        if (StrUtil.isNotBlank(productCode)) {
            payProductService.editStatus(productCode);
            return AjaxResult.ok();
        }

        return AjaxResult.fail(ResponseCode.REQUEST_PARMA_ERR);
    }


    @PostMapping("/payment/product/remove")
    public AjaxResult removeProduct(@RequestParam String productCode) {
        if (StrUtil.isNotBlank(productCode)) {
            payProductService.removeProduct(productCode);
            return AjaxResult.ok();
        }

        return AjaxResult.fail(ResponseCode.REQUEST_PARMA_ERR);
    }


    //模糊查询产品名称对象
    @GetMapping("/user/match/productName")
    public AjaxResult matchProductName(@RequestParam(value = "productName")String productName){
        if(StrUtil.isNotBlank(productName) && productName.trim().length() > 1){
            List<PayProduct> payProductList = payProductService.matchProductName(productName);
            List<MatchPayProductVo> listMatchPayProductVo = paymentConvert.toListMatchPayProductVo(payProductList);
            return AjaxResult.ok(listMatchPayProductVo);
        }
        return AjaxResult.fail(ResponseCode.REQUEST_PARMA_ERR);
    }

}
